package detectCycle

type ListNode struct {
	Val  int
	Next *ListNode
}

func DetectCycle(head *ListNode) *ListNode {
	if head == nil || head.Next == nil || head.Next.Next == nil {
		return nil
	}
	fastP := head.Next.Next
	slowP := head.Next
	for fastP != slowP {
		if fastP.Next == nil || fastP.Next.Next == nil {
			return nil
		}
		fastP = fastP.Next.Next
		slowP = slowP.Next
	}
	fastP = head
	for fastP != slowP {
		fastP = fastP.Next
		slowP = slowP.Next
	}
	return fastP
}
